//
//  YHBaseNavigationController.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/20.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHBaseNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 解决右滑返回手势问题
        //bug:在顶级页面,右滑返回手势还会响应
        self.interactivePopGestureRecognizer?.delegate = self
        
//        navigationController?.navigationBar.setBackgroundImage(UIImage(named: ), forBarMetrics: .Default)
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        return childViewControllers.count > 1
    }

    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        var backTitle = "返回"
        
        if childViewControllers.count > 0 {
            
            if childViewControllers.count == 1 {
                
                backTitle = childViewControllers.first?.title ?? "返回"
                
                
            }
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(popToFrontVC))
            viewController.navigationItem.leftBarButtonItem = back
            viewController.hidesBottomBarWhenPushed = true
        }
      
        

        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToFrontVC() {
        //本身就可以进行pop操作,不需要再通过属性navigationController进行操作
        popViewControllerAnimated(true)
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
